package com.ciko.code.views.calendar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.bean.MonthAttendsBean;
import com.ciko.code.utils.DensityUtils;
import com.mywork.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 日历GridView中的每一个item显示的TextView
 *
 * @author Hesky
 */
public class CalendarAdapter extends BaseAdapter {
    private boolean isLeapyear = false;  //是否为闰年
    private int daysOfMonth = 0;      //某月的天数
    private int dayOfWeek = 0;        //具体某一天是星期几
    private int lastDaysOfMonth = 0;  //上一个月的总天数
    private Context context;
    private String[] dayNumber = new String[42];  //一个GridView中的日期存入此数组中
    //	private static String week[] = {"周日","周一","周二","周三","周四","周五","周六"};
    private SpecialCalendar sc = null;
    private LunarCalendar lc = null;


    private String currentYear = "";
    private String currentMonth = "";
//	private String currentDay = "";

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d", Locale.CHINA);
    private int currentFlag = -1;     //用于标记当天

    private String showYear = "";   //用于在头部显示的年份
    private String showMonth = "";  //用于在头部显示的月份
    private String animalsYear = "";
    private String leapMonth = "";   //闰哪一个月
    private String cyclical = "";   //天干地支
    //当前时间
    private String sysDate = "";
    private String sys_year = "";
    private String sys_month = "";
    private String sys_day = "";

    List<MonthAttendsBean> list ;

    public CalendarAdapter(Date date) {
        sysDate = sdf.format(date);  //当期日期
        sys_year = sysDate.split("-")[0];
        sys_month = sysDate.split("-")[1];
        sys_day = sysDate.split("-")[2];

    }

    public CalendarAdapter(Context context,List<MonthAttendsBean> list){
        this.context = context;
        this.list = list;
    }


    public CalendarAdapter(Context context, Resources rs, Date date, int jumpMonth, int jumpYear, int year_c, int month_c, int day_c) {
        this(date);
        this.context = context;
        sc = new SpecialCalendar();
        lc = new LunarCalendar();

        int stepYear = year_c + jumpYear;
        int stepMonth = month_c + jumpMonth;
        if (stepMonth > 0) {
            //往下一个月滑动
            if (stepMonth % 12 == 0) {
                stepYear = year_c + stepMonth / 12 - 1;
                stepMonth = 12;
            } else {
                stepYear = year_c + stepMonth / 12;
                stepMonth = stepMonth % 12;
            }
        } else {
            //往上一个月滑动
            stepYear = year_c - 1 + stepMonth / 12;
            stepMonth = stepMonth % 12 + 12;
            if (stepMonth % 12 == 0) {

            }
        }

        currentYear = String.valueOf(stepYear);
        ;  //得到当前的年份
        currentMonth = String.valueOf(stepMonth);  //得到本月 （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
//		currentDay = String.valueOf(day_c);  //得到当前日期是哪天

        getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth));

    }

    public CalendarAdapter(Context context, Resources rs, Date date, int year, int month, int day) {
        this(date);
        this.context = context;
        sc = new SpecialCalendar();
        lc = new LunarCalendar();
//		this.res = rs;
        currentYear = String.valueOf(year);
        ;  //得到跳转到的年份
        currentMonth = String.valueOf(month);  //得到跳转到的月份
//		currentDay = String.valueOf(day);  //得到跳转到的天

        getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth));

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MonthAttendsBean item = list.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.calendar_item, parent, false);

        }
        RelativeLayout layout = (RelativeLayout) convertView.findViewById(R.id.layout_body);
        AbsListView.LayoutParams params = (AbsListView.LayoutParams) layout.getLayoutParams();
        params.height = DensityUtils.dp2px(context, 40);
        layout.setLayoutParams(params);


        TextView tvToday = (TextView) convertView.findViewById(R.id.tv_today);
        TextView textView = (TextView) convertView.findViewById(R.id.tvtext);
        TextView tvZaoTui = (TextView) convertView.findViewById(R.id.tv_zaotui);
        TextView tvQingjiaChuchai = (TextView) convertView.findViewById(R.id.tv_qingjia_chuchai);
        tvZaoTui.setVisibility(View.GONE);
        tvQingjiaChuchai.setVisibility(View.GONE);
//        String d = dayNumber[position].split("\\.")[0];
//		String dv = dayNumber[position].split("\\.")[1];
        String d = item.getDay();
        SpannableString sp = new SpannableString(d);
        sp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0, d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        sp.setSpan(new RelativeSizeSpan(1.2f), 0, d.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//		if(dv != null ||dv != ""){
//			sp.setSpan(new RelativeSizeSpan(0.75f), d.length()+1, dayNumber[position].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//		}
//		sp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 14, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        textView.setText(sp);
        textView.setTextColor(Color.GRAY);

//		if(position<7){
//			//设置周
//			textView.setTextColor(Color.WHITE);
//			textView.setBackgroundColor(color.search_txt_color);
//			textView.setTextSize(14);
//		}


        if (item.getDayType() == MonthAttendsBean.DayType.currentMonth) {
            // 当前月信息显示
            layout.setBackgroundColor(Color.WHITE);
            textView.setTextColor(Color.BLACK);// 当月字体设黑
            tvToday.setVisibility(View.GONE);

        }


        if (item.getDayType() == MonthAttendsBean.DayType.LastMonth) {
            // 当上月信息显示
            layout.setBackgroundColor(context.getResources().getColor(R.color.calender_g));
            textView.setTextColor(Color.BLACK);// 当月字体设黑
            tvToday.setVisibility(View.GONE);

        }
        if (item.getDayType() == MonthAttendsBean.DayType.toDay) {
            //设置当天的背景
            layout.setBackgroundColor(Color.WHITE);
            textView.setTextColor(Color.RED);
            tvToday.setVisibility(View.VISIBLE);
            tvToday.setText("今");
        }

        if (item.getUserEty() !=null) {
            //设置当天的背景
            layout.setBackgroundColor(Color.WHITE);
            textView.setTextColor(Color.BLACK);
            switch (item.getUserEty().getAttendType())
            {
                case 0:
                    tvQingjiaChuchai.setVisibility(View.GONE);
                    if(item.getUserEty().getReachType()==0) {
                        textView.setTextColor(0xff32da55);
                        switch (item.getUserEty().getQuitType()) {
                            case 0:
                                textView.setTextColor(0xff32da55);
                                tvZaoTui.setVisibility(View.GONE);
                                break;
                            case 2:
                                tvZaoTui.setVisibility(View.VISIBLE);
                                tvZaoTui.setTextColor(0xff2989ff);
                                break;
                            case 3:
                                break;
                        }
                    }
                    if(item.getUserEty().getReachType()==1) {
                        switch (item.getUserEty().getQuitType()) {
                            case 0:
                                tvZaoTui.setVisibility(View.GONE);
                                tvToday.setVisibility(View.VISIBLE);
                                tvToday.setText("迟到");
                                tvToday.setTextColor(0xffff1a35);
                                break;
                            case 2:
                                tvZaoTui.setVisibility(View.VISIBLE);
                                tvZaoTui.setTextColor(0xff2989ff);
                                tvToday.setVisibility(View.VISIBLE);
                                tvToday.setText("迟到");
                                tvToday.setTextColor(0xffff1a35);
                                break;
                            case 3:
                                break;
                            default:
                                tvZaoTui.setVisibility(View.GONE);
                                tvToday.setVisibility(View.VISIBLE);
                                tvToday.setText("迟到");
                                tvToday.setTextColor(0xffff1a35);
                                break;
                        }

                    }

//                    } else if(item.getUserEty().getReachType()==1){
//                        tvToday.setText("迟");
//                    } else if(item.getUserEty().getReachType()==2){
//                        tvToday.setText("退");
//                    } else if(item.getUserEty().getReachType()==3){

//                    }
                    break;
                case 1:
                    tvToday.setVisibility(item.getDayType() == MonthAttendsBean.DayType.toDay?View.VISIBLE:View.GONE);
                    tvQingjiaChuchai.setVisibility(View.VISIBLE);
                    tvQingjiaChuchai.setText("出差");
                    tvQingjiaChuchai.setTextColor(0xffad4623);
                    break;
                case 2:
                    tvToday.setVisibility(item.getDayType() == MonthAttendsBean.DayType.toDay?View.VISIBLE:View.GONE);
                    tvQingjiaChuchai.setVisibility(View.VISIBLE);
                    tvQingjiaChuchai.setText("请假");
                    tvQingjiaChuchai.setTextColor(0xffffa11a);
                    break;
                case 3:
                    tvToday.setVisibility(item.getDayType() == MonthAttendsBean.DayType.toDay?View.VISIBLE:View.GONE);
                    tvQingjiaChuchai.setVisibility(View.VISIBLE);
                    tvQingjiaChuchai.setText("其它");
                    tvQingjiaChuchai.setTextColor(0xff1ed90f);
                    break;
            }
//            switch (item.getUserEty().getAttendType())
//            {
//                case 0:
//                    if(item.getUserEty().getReachType()==0){
//                        textView.setTextColor(0xff32da55);
//                    } else if(item.getUserEty().getReachType()==1){
//                        tvToday.setText("迟到");
//                        tvToday.setTextColor(0xffff1a35);
//                    } else if(item.getUserEty().getReachType()==2){
//                        tvToday.setText("早退");
//                        tvToday.setTextColor(0xff2989ff);
//                    } else if(item.getUserEty().getReachType()==3){
//                        textView.setTextColor(0xff32da55);
//                    }
//                    break;
//                case 1:
//                    tvToday.setText("出差");
//                    tvToday.setTextColor(0xffad4632);
//                    break;
//                case 2:
//                    tvToday.setText("请假");
//                    tvToday.setTextColor(0xffffa11a);
//                    break;
//            }
        }

//        if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
//            // 当前月信息显示
//            layout.setBackgroundColor(Color.WHITE);
//            textView.setTextColor(Color.BLACK);// 当月字体设黑
//
//        }
//        if (currentFlag == position) {
//            //设置当天的背景
//            layout.setBackgroundColor(Color.WHITE);
//            textView.setTextColor(Color.RED);
//            tvToday.setVisibility(View.VISIBLE);
//        }
        return convertView;
    }

    //得到某年的某月的天数且这月的第一天是星期几
    public void getCalendar(int year, int month) {
        isLeapyear = sc.isLeapYear(year);              //是否为闰年
        daysOfMonth = sc.getDaysOfMonth(isLeapyear, month);  //某月的总天数
        dayOfWeek = sc.getWeekdayOfMonth(year, month);      //某月第一天为星期几
        lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1);  //上一个月的总天数
        Log.d("DAY", isLeapyear + " ======  " + daysOfMonth + "  ============  " + dayOfWeek + "  =========   " + lastDaysOfMonth);
        getweek(year, month);
    }

    //将一个月中的每一天的值添加入数组dayNuber中
    private void getweek(int year, int month) {
        int j = 1;
//		int flag = 0;
        String lunarDay = "";

        //得到当前月的所有日程日期(这些日期需要标记)

        for (int i = 0; i < dayNumber.length; i++) {
            // 周一
//			if(i<7){
//				dayNumber[i]=week[i]+"."+" ";
//			}
            if (i < dayOfWeek) {  //前一个月
                int temp = lastDaysOfMonth - dayOfWeek + 1;
                lunarDay = lc.getLunarDate(year, month - 1, temp + i, false);
                dayNumber[i] = (temp + i) + "." + lunarDay;

            } else if (i < daysOfMonth + dayOfWeek) {   //本月
                String day = String.valueOf(i - dayOfWeek + 1);   //得到的日期
                lunarDay = lc.getLunarDate(year, month, i - dayOfWeek + 1, false);
                dayNumber[i] = i - dayOfWeek + 1 + "." + lunarDay;
                //对于当前月才去标记当前日期
                if (sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)) {
                    //标记当前日期
                    currentFlag = i;
                }
                setShowYear(String.valueOf(year));
                setShowMonth(String.valueOf(month));
                setAnimalsYear(lc.animalsYear(year));
                setLeapMonth(lc.leapMonth == 0 ? "" : String.valueOf(lc.leapMonth));
                setCyclical(lc.cyclical(year));
            } else {   //下一个月
                lunarDay = lc.getLunarDate(year, month + 1, j, false);
                dayNumber[i] = j + "." + lunarDay;
                j++;
            }
        }

        String abc = "";
        for (int i = 0; i < dayNumber.length; i++) {
            abc = abc + dayNumber[i] + ":";
        }
        Log.d("DAYNUMBER", abc);


    }


    public void matchScheduleDate(int year, int month, int day) {

    }

    /**
     * 点击每一个item时返回item中的日期
     *
     * @param position
     * @return
     */
    public String getDateByClickItem(int position) {
        return dayNumber[position];
    }

    /**
     * 在点击gridView时，得到这个月中第一天的位置
     *
     * @return
     */
    public int getStartPositon() {
        return dayOfWeek + 7;
    }

    /**
     * 在点击gridView时，得到这个月中最后一天的位置
     *
     * @return
     */
    public int getEndPosition() {
        return (dayOfWeek + daysOfMonth + 7) - 1;
    }

    public String getShowYear() {
        return showYear;
    }

    public void setShowYear(String showYear) {
        this.showYear = showYear;
    }

    public String getShowMonth() {
        return showMonth;
    }

    public void setShowMonth(String showMonth) {
        this.showMonth = showMonth;
    }

    public String getAnimalsYear() {
        return animalsYear;
    }

    public void setAnimalsYear(String animalsYear) {
        this.animalsYear = animalsYear;
    }

    public String getLeapMonth() {
        return leapMonth;
    }

    public void setLeapMonth(String leapMonth) {
        this.leapMonth = leapMonth;
    }

    public String getCyclical() {
        return cyclical;
    }

    public void setCyclical(String cyclical) {
        this.cyclical = cyclical;
    }
}
